package org.lex.ftp.ui.bookmark;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;

import org.lex.awt.ComponentUtilities;
import org.lex.ftp.ui.URLPane;
import org.lex.ftp.util.bookmark.Site;
import org.lex.swing.DocumentAdapter;
import org.lex.swing.SelectAllWhenGainFocus;
import org.lex.swing.Swings;


public class BookmarkEditPane extends JPanel {
	private JPanel gluePane = null;
	private JLabel siteNameLabel = null;
	private JTextField siteNameTextField = null;
	private URLPane urlPane = null;
	// -----------------------------
	private boolean contentChanged = false;
	private Site site = null;

	public static void main(String[] args) {
		Swings.show(new BookmarkEditPane());
	}

	public BookmarkEditPane() {
		super();
		initialize();
		this.hookActionHandler();
	}

	public boolean isContentChanged() {
		return contentChanged || urlPane.isContentChanged();
	}

	public void setContentChanged(boolean contentChanged) {
		this.contentChanged = contentChanged;
		this.urlPane.setContentChanged(contentChanged);
	}

	public void setSite(Site site) {
		this.site = site;
		this.siteNameTextField.setText(site.getName());
		this.urlPane.setURL(site.getURL());
		ComponentUtilities.setEnabled(this, true);
		this.setContentChanged(false);
	}

	public void reset() {
		this.site = null;
		this.siteNameTextField.setText("");
		this.urlPane.reset();
		ComponentUtilities.setEnabled(this, false);
		this.setContentChanged(false);
	}

	public boolean saveChanges() {
		if (null != site && true == this.isContentChanged()) {
			site.setName(this.getSiteNameTextField().getText());
			site.setURL(urlPane.getURL());
			return true;
		} else
			return false;
	}

	private void hookActionHandler() {
		this.siteNameTextField.getDocument().addDocumentListener(new DocumentAdapter() {
			@Override
			public void documentChanged(DocumentEvent e) {
				contentChanged = true;
			}
		});
		this.siteNameTextField.addFocusListener(SelectAllWhenGainFocus.getInstance());
	}

	private void initialize() {
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.gridx = 0;
		gridBagConstraints11.gridwidth = 2;
		gridBagConstraints11.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints11.weightx = 0.5;
		gridBagConstraints11.weighty = 0.5;
		gridBagConstraints11.gridy = 2;
		GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
		gridBagConstraints2.gridx = 0;
		gridBagConstraints2.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints2.weightx = 1.0;
		gridBagConstraints2.gridwidth = 2;
		gridBagConstraints2.gridy = 1;
		GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
		gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints1.gridy = 0;
		gridBagConstraints1.weightx = 1.0;
		gridBagConstraints1.insets = new Insets(0, 0, 11, 0);
		gridBagConstraints1.gridx = 1;
		GridBagConstraints gridBagConstraints = new GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.insets = new Insets(0, 0, 11, 5);
		gridBagConstraints.anchor = GridBagConstraints.WEST;
		gridBagConstraints.gridy = 0;
		siteNameLabel = new JLabel();
		siteNameLabel.setText("Site Name");
		// ===================================
		this.setLayout(new GridBagLayout());
		this.setSize(new Dimension(408, 298));
		this.add(siteNameLabel, gridBagConstraints);
		this.add(getSiteNameTextField(), gridBagConstraints1);
		this.add(getUrlPane(), gridBagConstraints2);
		this.add(getGluePane(), gridBagConstraints11);
	}

	private JTextField getSiteNameTextField() {
		if (siteNameTextField == null) {
			siteNameTextField = new JTextField();
		}
		return siteNameTextField;
	}

	private URLPane getUrlPane() {
		if (urlPane == null) {
			urlPane = new URLPane();
		}
		return urlPane;
	}

	private JPanel getGluePane() {
		if (gluePane == null) {
			gluePane = new JPanel();
		}
		return gluePane;
	}
} // @jve:decl-index=0:visual-constraint="10,10"
